package com.ming.controller.admin;

import com.ming.dto.DishDTO;
import com.ming.dto.DishPageQueryDTO;
import com.ming.entity.Dish;
import com.ming.result.PageResult;
import com.ming.result.Result;
import com.ming.service.DishService;
import com.ming.vo.DishVO;
import com.ming.vo.DishVOWithFlavors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavors(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVOWithFlavors> getById(@PathVariable Long id) {
        log.info("根据ID查询菜品：{}", id);
        DishVOWithFlavors dishVOWithFlavors = dishService.getByIdWithFlavors(id);
        return Result.success(dishVOWithFlavors);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavors(dishDTO);
        return Result.success();
    }

    /**
     * 根据ID删除菜品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        log.info("根据ID删除菜品：{}", id);
        dishService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除菜品：{}", ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 启用或禁用菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, @RequestParam Long id) {
        log.info("{}菜品：{}", status == 1 ? "启用" : "禁用", id);
        dishService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据分类ID查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishVO>> getByCategoryId(@RequestParam Long categoryId) {
        log.info("根据分类ID查询菜品：{}", categoryId);
        List<DishVO> dishVOs = dishService.getByCategoryId(categoryId);
        return Result.success(dishVOs);
    }
}